package com.saturday.data.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.saturday.data.scope.impl.AreaDataPermissionHandler;
import com.github.pagehelper.PageInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static org.springframework.core.Ordered.LOWEST_PRECEDENCE;

@Configuration
@Component
public class SaturdayMybatisPlusConfig {

    /**
     * 加载在MybatisPlusInterceptor之前否则导致分页total偏差
     *
     * @return
     */
    @Bean
    @Order(LOWEST_PRECEDENCE - 5)
    public PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        PageHelperProperties pageHelperProperties = new PageHelperProperties();
        pageHelperProperties.setParams("pageNum=page;pageSize=limit;");
        pageHelperProperties.setSupportMethodsArguments(true);
        pageInterceptor.setProperties(pageHelperProperties);
        return pageInterceptor;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new DataPermissionInterceptor(new AreaDataPermissionHandler()));
        return interceptor;
    }


}
